package org.BUPT.admin.system.controller;

import org.BUPT.admin.system.validator.SubjectValid;
import org.BUPT.common.utils.ResultVoUtil;
import org.BUPT.common.vo.ResultVo;
import org.BUPT.modules.system.domain.Role;
import org.BUPT.modules.system.domain.Subject;
import org.BUPT.modules.system.service.SubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理界面
 */

@Controller
@RequestMapping("/system/class")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping("/index")
    @RequiresPermissions("system:class:index")
    public String index(Model model, Subject subject){
        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching().
                withMatcher("name", match -> match.contains());

        // 获取角色列表
        Example<Subject> example = Example.of(subject, matcher);
        Page<Subject> list = subjectService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/system/class/index1";
    }


    @GetMapping("/addsubject")
    @RequiresPermissions("system:class:add")
    public String toAddSubject(){
        return "/system/class/add";
    }


    @GetMapping("/studentlist/{id}")
    @RequiresPermissions("system:class:add")
    public String toShowStudents(@PathVariable("id") Subject subject, Model model){
        model.addAttribute("list", subjectService.getSutents(subject.getId()));
        model.addAttribute("subjectid",subject.getId());
        return "/system/class/studentlist";
    }

    @GetMapping("/teacherlist/{id}")
    @RequiresPermissions("system:class:add")
    public String toShowTeachers(@PathVariable("id") Subject subject, Model model){
        model.addAttribute("list", subjectService.getTeachers(subject.getId()));
        model.addAttribute("subjectid",subject.getId());
        return "/system/class/teacherlist";
    }

    @PostMapping("/realaddstu")
    @ResponseBody
    public ResultVo toAddStudentForSubject(@RequestParam("data[]") List<String> studentIds,@RequestParam("id") String subjectid){
        Subject subject=subjectService.getSubject(Long.valueOf(subjectid));
        for(String id:studentIds){
            subjectService.addStudent(subject.getId(), Long.valueOf(id));
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }



    @GetMapping("/addstudentforsubject/{id}")
    public String addstudentforsubject(@PathVariable("id") Subject subject,Model model){
        model.addAttribute("list", subjectService.getSutentsNotIn(subject.getId()));
        model.addAttribute("subjectid",subject.getId());
        return "/system/class/addstudent";
    }


    @PostMapping("/add")
    @RequiresPermissions({"system:role:add", "system:role:edit"})
    @ResponseBody
    public ResultVo toSave(@Validated SubjectValid valid,Subject subject){
        subjectService.save(subject);
        return ResultVoUtil.SAVE_SUCCESS;
    }





}
